package com.servitware.cardio.dokstat.edf;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class EdfReader
{

	public EdfReader()
	{
	}

	public EdfHeader getHeader()
	{

		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		/*
		 * 8 ascii : version of this data format (0) 80 ascii : local patient
		 * identification (mind item 3 of the additional EDF+ specs) 80 ascii :
		 * local recording identification (mind item 4 of the additional EDF+
		 * specs) 8 ascii : startdate of recording (dd.mm.yy) (mind item 2 of
		 * the additional EDF+ specs) 8 ascii : starttime of recording
		 * (hh.mm.ss) 8 ascii : number of bytes in header record 44 ascii :
		 * reserved 8 ascii : number of data records (-1 if unknown, obey item
		 * 10 of the additional EDF+ specs) 8 ascii : duration of a data record,
		 * in seconds 4 ascii : number of signals (ns) in data record
		 */

		byte[] version = new byte[8];
		byte[] patientIdentification = new byte[80];
		byte[] localRecordingIdentification = new byte[80];
		byte[] startDateOfRecording = new byte[8];
		byte[] startTimeOfRecording = new byte[8];
		byte[] numberOfBytesInHeaderRecord = new byte[8];
		byte[] reserved = new byte[44];
		byte[] numberOfDataRecords = new byte[8];
		byte[] durationOfADataRecord = new byte[8];
		byte[] numberOfSignalsInDataRecord = new byte[4];

		File file = new File("/home/erik/work/sandbox/dokstat/_ds-en-proceso/80.edf");
		InputStream input = null;

		try
		{
			int bytesRead = 0;

			input = new BufferedInputStream(new FileInputStream(file));

			bytesRead = input.read(version, bytesRead, 8);
			bytesRead = input.read(patientIdentification); //, bytesRead, 80);
			bytesRead = input.read(localRecordingIdentification); //, bytesRead, 80);
			bytesRead = input.read(startDateOfRecording); //, bytesRead, 8);
			bytesRead = input.read(startTimeOfRecording); //, bytesRead, 8);
			bytesRead = input.read(numberOfBytesInHeaderRecord); //, bytesRead, 8);
			bytesRead = input.read(reserved); //, bytesRead, 44);
			bytesRead = input.read(numberOfDataRecords); //, bytesRead, 8);
			bytesRead = input.read(durationOfADataRecord); //, bytesRead, 8);
			bytesRead = input.read(numberOfSignalsInDataRecord); //, bytesRead, 4);

			String sVersion = new String( version );
			System.out.println( "Version : " + sVersion );
			
			String sStartDateOfRecording = new String( startDateOfRecording );
			System.out.println("Fecha inicio grabacion; " + sStartDateOfRecording );
			
			String sStartTimeOfRecording = new String( startTimeOfRecording );
			System.out.println("Hora inicio grabacion; " + sStartTimeOfRecording );
		
			Integer numberOfDataRecordsInteger = new Integer( new String(numberOfDataRecords).trim() );
			Integer durationOfADataRecordInteger = new Integer ( new String( durationOfADataRecord ).trim() );
			
			int minutos = numberOfDataRecordsInteger.intValue() * durationOfADataRecordInteger.intValue() / 60;
			System.out.println("Duracion grabacion; " + String.valueOf( minutos ) );
				
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				input.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}

	}
}
